﻿using Helper;
using InterfaceService;
using Microsoft.AspNetCore.Mvc;
using Model.Request;
using Model.Response;

namespace Vue3Net7.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class LoginController : ControllerBase
    {   
        private readonly ILogger<LoginController> _logger;
        private readonly IUserService _userService;
        private readonly ICustomJWTService _customJWTService;
        public LoginController(ILogger<LoginController> logger, IUserService userService, ICustomJWTService customJWTService)
        {
            _logger = logger;
            _userService = userService;
            _customJWTService = customJWTService;
        }

        [HttpPost]
        public async Task<ApiResult> GetToken([FromBody] LoginReq req)
        {
            if (ModelState.IsValid)
            {
                UserRes user = await _userService.GetUser(req);
                if(user == null)
                    return ApiResultHelper.Error("用户不存在，用户名或密码错误！");
                _logger.LogInformation("用户登录成功！");
                return ApiResultHelper.Success(await _customJWTService.GetToken(user));
            }
            else
                return ApiResultHelper.Error("参数错误！");
        }
    }
}
